home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cxref_1_4a.lha / comment.c < prev    next >
C/C++ Source or Header  |  1997-12-07  |  6KB  |  294 lines

  1. /***************************************
  2.   $Header: /home/amb/cxref/RCS/comment.c 1.16 1997/11/20 20:14:20 amb Exp $
  3.  
  4.   C Cross Referencing & Documentation tool. Version 1.4a.
  5.  
  6.   Collects the comments from the parser.
  7.   ******************/ /******************
  8.   Written by Andrew M. Bishop
  9.  
  10.   This file Copyright 1995,96 Andrew M. Bishop
  11.   It may be distributed under the GNU Public License, version 2, or
  12.   any higher version.  See section COPYING of the GNU Public license
  13.   for conditions under which this file may be redistributed.
  14.   ***************************************/
  15.  
  16. /*+ Turn on the debugging in this file. +*/
  17. #define DEBUG 0
  18.  
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22.  
  23. #include "memory.h"
  24. #include "datatype.h"
  25. #include "cxref.h"
  26.  
  27. #ifdef AMIGA    /* olsen */
  28. #include "amiga.h"
  29. #endif /* AMIGA */
  30.  
  31. static void TidyCommentString(char **string);
  32.  
  33.  
  34. /*+ The file that is currently being processed. +*/
  35. extern File CurFile;
  36.  
  37. /*+ The current (latest comment). +*/
  38. static char* cur_comment=NULL;
  39.  
  40.  
  41. /*++++++++++++++++++++++++++++++++++++++
  42.   Function that is called when a comment or part of one is seen. The comment is built up until an end of comment is signaled.
  43.  
  44.   char* c The comment text. If c==0 then it is a file (/ * * comment * * /) comment
  45.                             if c==1 then it is the other special comment (/ * + comment + * /).
  46.                             if c==2 then it is a normal comment (/ * comment * /).
  47.                             if c==3 then it is not a comment.
  48.   ++++++++++++++++++++++++++++++++++++++*/
  49.  
  50. void SeenComment(char* c)
  51. {
  52.  static int comment_ended=0;
  53.  
  54.  switch((int)c)
  55.    {
  56.    case 0:
  57. #if DEBUG
  58.     printf("#Comment.c# Seen comment /**\n%s\n**/\n",cur_comment);
  59. #endif
  60.     TidyCommentString(&cur_comment);
  61.     if(!CurFile->comment)
  62.        SeenFileComment(cur_comment);
  63.     cur_comment=NULL;
  64.     comment_ended=1;
  65.     break;
  66.  
  67.    case 1:
  68. #if DEBUG
  69.     printf("#Comment.c# Seen comment /*+\n%s\n+*/\n",cur_comment);
  70. #endif
  71.     TidyCommentString(&cur_comment);
  72.     if(SeenFuncIntComment(cur_comment))
  73.        cur_comment=NULL;
  74.     comment_ended=1;
  75.     break;
  76.  
  77.    case 2:
  78. #if DEBUG
  79.     printf("#Comment.c# Seen comment /*\n%s\n*/\n",cur_comment);
  80. #endif
  81.     TidyCommentString(&cur_comment);
  82.     if(!CurFile->comment)
  83.       {SeenFileComment(cur_comment); cur_comment=NULL;}
  84.     comment_ended=1;
  85.     break;
  86.  
  87.    case 3:
  88.     comment_ended=0; cur_comment=NULL;
  89.     break;
  90.  
  91.    default:
  92.     if(comment_ended)
  93.       {comment_ended=0; cur_comment=NULL;}
  94.  
  95.     cur_comment=ConcatStrings(2,cur_comment,c);
  96.    }
  97. }
  98.  
  99.  
  100. /*++++++++++++++++++++++++++++++++++++++
  101.   Provide the current (latest) comment.
  102.  
  103.   char* GetCurrentComment Returns the current (latest) comment.
  104.   ++++++++++++++++++++++++++++++++++++++*/
  105.  
  106. char* GetCurrentComment(void)
  107. {
  108.  char* c=cur_comment;
  109.  
  110. #if DEBUG
  111.     printf("#Comment.c# GetCurrentComment returns <<<%s>>>\n",cur_comment);
  112. #endif
  113.  
  114.  cur_comment=NULL;
  115.  
  116.  return(c);
  117. }
  118.  
  119.  
  120. /*++++++++++++++++++++++++++++++++++++++
  121.   Set the current (latest) comment.
  122.  
  123.   char* comment The comment.
  124.   ++++++++++++++++++++++++++++++++++++++*/
  125.  
  126. void SetCurrentComment(char* comment)
  127. {
  128. #if DEBUG
  129.     printf("#Comment.c# SetCurrentComment set to <<<%s>>>\n",comment);
  130. #endif
  131.  
  132.  cur_comment=comment;
  133. }
  134.  
  135.  
  136. /*++++++++++++++++++++++++++++++++++++++
  137.   A function to split out the arguments etc from a comment,
  138.   for example the function argument comments are separated using this.
  139.  
  140.   char* SplitComment Returns the required comment.
  141.  
  142.   char** original A pointer to the original comment, this is altered in the process.
  143.  
  144.   char* name The name that is to be cut out from the comment.
  145.  
  146.   A most clever function that ignores spaces so that 'char* b' and 'char *b' match.
  147.   ++++++++++++++++++++++++++++++++++++++*/
  148.  
  149. char* SplitComment(char** original,char* name)
  150. {
  151.  char* c=NULL;
  152.  
  153.  if(*original)
  154.    {
  155.     int l=strlen(name);
  156.     c=*original;
  157.  
  158.     do{
  159.        int i,j,failed=0;
  160.        char* start=c;
  161.  
  162.        while(c[0]=='\n')
  163.           c++;
  164.  
  165.        for(i=j=0;i<l;i++,j++)
  166.          {
  167.           while(name[i]==' ') i++;
  168.           while(c[j]==' ') j++;
  169.  
  170.           if(!c[j] || name[i]!=c[j])
  171.             {failed=1;break;}
  172.          }
  173.  
  174.        if(!failed)
  175.          {
  176.           char* old=*original;
  177.           char* end=strstr(c,"\n\n");
  178.           *start=0;
  179.           if(end)
  180.              *original=MallocString(ConcatStrings(2,*original,end));
  181.           else
  182.              if(start==*original)
  183.                 *original=NULL;
  184.              else
  185.                 *original=MallocString(*original);
  186.           if(end)
  187.              *end=0;
  188.           c=CopyString(&c[j+1]);
  189.           Free(old);
  190.           break;
  191.          }
  192.       }
  193.     while((c=strstr(c,"\n\n")));
  194.    }
  195.  
  196.  return(c);
  197. }
  198.  
  199.  
  200. /*++++++++++++++++++++++++++++++++++++++
  201.   Tidy up the current comment string by snipping off trailing and leading junk.
  202.  
  203.   char **string The string that is to be tidied.
  204.   ++++++++++++++++++++++++++++++++++++++*/
  205.  
  206. static void TidyCommentString(char **string)
  207. {
  208.  int whitespace;
  209.  char *to=*string,*from=*string,*str;
  210.  
  211.  if(!*string)
  212.     return;
  213.  
  214.  /* Remove CR characters. */
  215.  
  216.  while(*from)
  217.    {
  218.     if(*from=='\r')
  219.        from++;
  220.     else
  221.        *to++=*from++;
  222.    }
  223.  *to=0;
  224.  
  225.  /* Remove leading blank lines. */
  226.  
  227.  whitespace=1;
  228.  str=*string;
  229.  do
  230.    {
  231.     if(*str!='\n')
  232.        do
  233.          {
  234.           if(*str!=' ' && *str!='\t')
  235.              whitespace=0;
  236.          }
  237.        while(*str && *++str!='\n');
  238.  
  239.     if(whitespace)
  240.        *string=++str;
  241.    }
  242.  while(whitespace);
  243.  
  244.  /* Remove trailing blank lines. */
  245.  
  246.  whitespace=1;
  247.  str=*string+strlen(*string)-1;
  248.  do
  249.    {
  250.     if(*str!='\n')
  251.        do
  252.          {
  253.           if(*str!=' ' && *str!='\t')
  254.              whitespace=0;
  255.          }
  256.        while(str>*string && *--str!='\n');
  257.  
  258.     if(whitespace)
  259.        *str--=0;
  260.    }
  261.  while(whitespace);
  262.  
  263.  /* Replace lines containing just whitespace with empty lines. */
  264.  
  265.  str=*string;
  266.  do
  267.    {
  268.     char *start;
  269.  
  270.     whitespace=1;
  271.  
  272.     while(*str=='\n')
  273.        str++;
  274.  
  275.     start=str;
  276.  
  277.     while(*str && *++str!='\n')
  278.        {
  279.         if(*str!=' ' && *str!='\t')
  280.            whitespace=0;
  281.        }
  282.  
  283.     if(whitespace)
  284.       {
  285.        char *copy=start;
  286.  
  287.        while((*start++=*str++));
  288.  
  289.        str=copy;
  290.       }
  291.    }
  292.  while(*str);
  293. }
  294.